from django.shortcuts import render
from django.views import View
from unicodedata import category

from meidou_mall.apps.contents.models import ContentCategory
from meidou_mall.apps.goods.models import GoodsChannel
from meidou_mall.utils.my_category import get_categories


# Create your views here.
class IndexView(View):
    def get(self, request):
        categories = get_categories()

        contents = {}
        content_categories = ContentCategory.objects.order_by("id").all()
        # for content_category in content_categories:
        #     contents[content_category.key] = content_category.content_set.all()

        for content_category in content_categories:
            sub_contents = content_category.content_set.all()
            sub_content_list = []
            for sub_content in sub_contents:
                sub_content_list.append({
                    "id": sub_content.id,
                    "title": sub_content.title,
                    "url": sub_content.url,
                    "image": sub_content.image,
                    "text": sub_content.text,
                    "sequence": sub_content.sequence,
                    "status": sub_content.status
                })

            contents[content_category.key] = sub_content_list


        print(categories)
        print(f"contents={contents}")
        context = {
            "categories": categories,
            "contents": contents
        }
        return render(request, 'index.html', context=context)


    # old code
    # def get(self, request):
    #     categories = {}
    #
    #     channels = GoodsChannel.objects.order_by("sequence").all()
    #     for channel in channels:
    #         group_id = channel.group_id
    #
    #         if group_id not in categories:
    #             categories[group_id] = {
    #                 "channels": [],
    #                 "sub_cats": []
    #             }
    #
    #         cat1 = channel.category
    #         cat1_dict = {
    #             "url": channel.url,
    #             "name": cat1.name,
    #         }
    #
    #         categories[group_id]["channels"].append(cat1_dict)
    #
    #         cats2 = cat1.subs.all()
    #         # for cat2 in cats2:
    #         #     categories[group_id]["sub_cats"].append({
    #         #         'name': cat2.name,
    #         #         'subs': cat2.subs.all()
    #         #     })
    #
    #         for cat2 in cats2:
    #             subs_list = []
    #             subs_data = cat2.subs.all()
    #             for sub in subs_data:
    #                 subs_list.append({
    #                     "id": sub.id,
    #                     "name": sub.name
    #                 })
    #             categories[group_id]["sub_cats"].append({
    #                 'name': cat2.name,
    #                 'subs': subs_list
    #             })
    #
    #     contents = {}
    #     content_categories = ContentCategory.objects.order_by("id").all()
    #     # for content_category in content_categories:
    #     #     contents[content_category.key] = content_category.content_set.all()
    #
    #     for content_category in content_categories:
    #         sub_contents = content_category.content_set.all()
    #         sub_content_list = []
    #         for sub_content in sub_contents:
    #             sub_content_list.append({
    #                 "id": sub_content.id,
    #                 "title": sub_content.title,
    #                 "url": sub_content.url,
    #                 "image": sub_content.image,
    #                 "text": sub_content.text,
    #                 "sequence": sub_content.sequence,
    #                 "status": sub_content.status
    #             })
    #
    #         contents[content_category.key] = sub_content_list
    #
    #
    #     print(categories)
    #     print(f"contents={contents}")
    #     context = {
    #         "categories": categories,
    #         "contents": contents
    #     }
    #     return render(request, 'index.html', context=context)



























